
/*!
 * \file      basicimagesquare.cpp
 * \brief     BasicImageSquare implementation.
 * \author    Duriez Franck
 * \author    Carton Paul-Henri
 * \version   0.1
 */

#include "basicimagesquare.h"
#include "assert_macros.h"
#include "constant.h"

BasicImageSquare::BasicImageSquare(QSFMLWidget *displayWidget, float *dt, sf::Image image) : /*{{{*/
	    Square(displayWidget, dt),
	    m_image(image),
	    m_time_elapsed_on_destruction(0.f)
{
	// Image is set in the sprite
	m_sprite.SetImage(m_image);
} /*}}}*/

BasicImageSquare::BasicImageSquare(QSFMLWidget *displayWidget, float *dt, std::string imageFile) : /*{{{*/
	    Square(displayWidget, dt),
	    m_time_elapsed_on_destruction(0.f)
{
	// Image is loaded from the imageFile
	m_image.LoadFromFile(imageFile);

	// Image is set in the sprite
	m_sprite.SetImage(m_image);
} /*}}}*/

BasicImageSquare::~BasicImageSquare() /*{{{*/
{
} /*}}}*/

void BasicImageSquare::setImage(sf::Image image) /*{{{*/
{
	// Set Image
	m_image = image;

	// Image is set in the sprite
	m_sprite.SetImage(m_image);
} /*}}}*/

void BasicImageSquare::setImage(std::string imageFile) /*{{{*/
{
	// Image is loaded from imageFile
	m_image.LoadFromFile(imageFile);

	// Image is set in the sprite
	m_sprite.SetImage(m_image);
} /*}}}*/

/************
 * DRAWABLE *
 ************/
/*{{{*/
void BasicImageSquare::draw() const /*{{{*/
{
	displayer->Draw(m_sprite);
	displayer->Draw(m_count);
} /*}}}*/

void BasicImageSquare::update() /*{{{*/
{
	Square::update();
} /*}}}*/

// absolute setters
void BasicImageSquare::setCenter(Vector2f center) /*{{{*/
{
	m_sprite.SetCenter(center);
} /*}}}*/

void BasicImageSquare::setPosition(Vector2f position) /*{{{*/
{
	m_sprite.SetPosition(position);
} /*}}}*/

void BasicImageSquare::setRotation(float angle) /*{{{*/
{
	m_sprite.SetRotation(angle);
//    DEBUGGER("m_sprite rotation : " << m_sprite.GetRotation());
//    DEBUGGER("square rotation  : " << angle);
} /*}}}*/

// relative setters
void BasicImageSquare::moveCenter(Vector2f dcenter) /*{{{*/
{
	Vector2f tmp = m_sprite.GetCenter() + dcenter;
	m_sprite.SetCenter(tmp);
} /*}}}*/

void BasicImageSquare::move(Vector2f dposition) /*{{{*/
{
	m_sprite.Move(dposition);
} /*}}}*/

void BasicImageSquare::rotate(float dangle) /*{{{*/
{
	m_sprite.Rotate(dangle);
} /*}}}*/

// getters
Vector2f BasicImageSquare::getCenter() const /*{{{*/
{
	return m_sprite.GetCenter();
} /*}}}*/

Vector2f BasicImageSquare::getPosition() const /*{{{*/
{
	return m_sprite.GetPosition();
} /*}}}*/

float BasicImageSquare::getRotation() const /*{{{*/
{
	return m_sprite.GetRotation();
} /*}}}*/

// description
std::string BasicImageSquare::toString() const /*{{{*/
{
	std::ostringstream res;
	res << "position              : (" << getPosition().x << ","
	        << getPosition().y << ")" << std::endl;
	res << "center                : (" << getCenter().x << ","
	        << getCenter().y << ")" << std::endl;
	res << "angle                 : " << getRotation() << std::endl;
	res << "position in container : (" << getPositionInContainer().x << ","
	        << getPositionInContainer().y << ")" << std::endl;
	return res.str();
} /*}}}*/
/*}}}*/

/***********
 * ACTIONS *
 ***********/
/*{{{*/
void BasicImageSquare::destroyStartingProcess() /*{{{*/
{
	// Nothing to do.
} /*}}}*/

void BasicImageSquare::destroyProcess() /*{{{*/
{
	if (m_time_elapsed_on_destruction <= 2*SQUARE_DESTRUCTION_DELAY)
	{
		destroy.maintain();
		std::ostringstream count;
		count << (int) ((2*SQUARE_DESTRUCTION_DELAY - m_time_elapsed_on_destruction)*10);
		m_count = sf::String(count.str(), sf::Font::GetDefaultFont(), 15.f);
		sf::FloatRect rect = m_count.GetRect();
		m_count.SetPosition(getPosition().x - rect.GetWidth()/2, getPosition().y - rect.GetHeight()/2);
		m_time_elapsed_on_destruction += *dt;
	}
} /*}}}*/

void BasicImageSquare::destroyStoppingProcess() /*{{{*/
{
	/// There are a default process : destroyDefaultStoppingProcess
	// Nothing to do otherwise.
} /*}}}*/
/*}}}*/

